#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 29 11:30:34 2021

Plot map of observation sites

@author: arifeinberg
"""
#%% functions to import
import os
os.chdir('/Users/arifeinberg/target2/fs03/d0/arifein/python/')

import xarray as xr
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import csv
from scipy import stats
from functions_Hg0 import get_mod_data, ds_sel_yr
import cartopy.crs as ccrs
import cartopy.feature as cf
from mpl_toolkits.basemap import Basemap
import seaborn as sns
#%% Load observed dry deposition velocities
source = '../obs_datasets/dry_dep/Dry_deposition_compilation_300921.csv'
data_dd_f = pd.read_csv(source)

df_dd = data_dd_f.loc[~data_dd_f['Litterfall Dry Deposition velocity (cm s-1)'].isna()].reset_index()
df_dd_litter_only = df_dd[~df_dd['Litterfall Dry Deposition velocity (cm s-1)'].isna() 
                           & df_dd['Dry deposition veloctiy (cm s-1)'].isna()]
# remove Amazon sites, add afterwards Fostier sites
df_dd_litter_only = df_dd_litter_only[df_dd_litter_only['Latitude (N)'] > 0]

df_dd_total= df_dd[~df_dd['Dry deposition veloctiy (cm s-1)'].isna()]

source_2 = '../obs_datasets/dry_dep/Amazon_Litter_Fostier.csv'
data_amazon = pd.read_csv(source_2)
obs_lat_amazon = data_amazon['Latitude (N)'].values
obs_lon_amazon = data_amazon['Longitude (E)'].values

#remove Obrist study from total
df_dd_total = df_dd_total.iloc[:-1]
df_dd_obr = df_dd_total.iloc[-1]

#%% Make map plot of litterfall/dry deposition datasets
fig = plt.figure(figsize=(8, 6), edgecolor='w')
m = Basemap(projection='eck4', resolution='c',
            lat_0=0, lon_0=0,area_thresh=100000)
m.bluemarble(scale=1)
m.drawlsmask(land_color='none', ocean_color="#050F29",
              resolution = 'l', lakes=True)
#m.drawcoastlines(linewidth=1)
h1 = m.scatter(df_dd_litter_only['Longitude (E)'].tolist(), df_dd_litter_only['Latitude (N)'].tolist(), s=50,
                      c='white', marker="o",linewidths=1.5, edgecolors='black', latlon=True,
                      label="Litterfall",zorder=20)
h2 = m.scatter(df_dd_total['Longitude (E)'].tolist(), df_dd_total['Latitude (N)'].tolist(), s=50,
                      c='#e41a1c',marker="o", linewidths=1.5, edgecolors='black', latlon=True,
                      label='Litterfall, throughfall, & wet dep',zorder=21)
h3 = m.scatter(df_dd_obr['Longitude (E)'].tolist(), df_dd_obr['Latitude (N)'].tolist(), s=150,
                      c='#377eb8',marker="*", linewidths=1., edgecolors='black', latlon=True,
                      label='Flux tower (Obrist et al., 2021)',zorder=24)
h4 = m.scatter(obs_lon_amazon, obs_lat_amazon, s=50,
                      c='white', marker="o",linewidths=1.5, edgecolors='black', latlon=True, zorder=20)
plt.legend(bbox_to_anchor=(0, 1.02, 1, 0.2), loc='lower left', mode = "expand", 
           frameon=False, ncol=1, fontsize=18)
fig.savefig('Figures/Litter_dep_data_sitemap.pdf',bbox_inches = 'tight')
#%% Make map of Hg0 observation sites
AnHgObs= pd.read_csv('pythonHgBenchmark/data/TGMSiteAnnual.csv',skiprows=[0], na_values=(-9999))
AnHgObs.columns=['SiteID', 'Lat', 'Lon','Alt', 'TGM', 'Hg0']
source = '../obs_datasets/GEM/Passive_sampler_toronto.csv'
data_SA = pd.read_csv(source)
    
fig = plt.figure(figsize=(8, 6), edgecolor='w')
m = Basemap(projection='eck4', resolution='c',
            lat_0=0, lon_0=0,area_thresh=100000)
m.bluemarble(scale=1)
m.drawlsmask(land_color='none', ocean_color="#050F29",
              resolution = 'l', lakes=True)
#m.drawcoastlines(linewidth=1)
h1 = m.scatter(AnHgObs['Lon'].tolist(), AnHgObs['Lat'].tolist(), s=50,
                      c='white', marker="o",linewidths=1.5, edgecolors='black', latlon=True,
                      label="Hg$^{0}$ measurement station",zorder=20)
h2 = m.scatter(data_SA['Longitude'].tolist(), data_SA['Latitude'].tolist(), s=50,
                      c='white', marker="s",linewidths=1.5, edgecolors='black', latlon=True,
                      label="LAPAN passive sampler",zorder=20)

plt.legend(bbox_to_anchor=(0, 1.02, 1, 0.2), loc='lower left', mode = "expand", 
           frameon=False, ncol=1, fontsize=18)
fig.savefig('Figures/Hg0_data_sitemap.pdf',bbox_inches = 'tight')


